#include "qresourcetreeview.h"
#include "qresourcelistview.h"

#include <QHeaderView>
#include <QTreeWidgetItem>


QResourceTreeView::QResourceTreeView(AbstractCore *core, QWidget *parent):
    QTreeWidget(parent),
    m_core(core)
{
    setFocusPolicy(Qt::NoFocus);
    setFrameStyle(QFrame::NoFrame);
    setIndentation(0);
    setRootIsDecorated(false);
    setColumnCount(1);
    header()->hide();
    header()->setResizeMode(QHeaderView::Stretch);
    setTextElideMode(Qt::ElideMiddle);
    setVerticalScrollMode(ScrollPerPixel);



    m_listView=new QResourceListView(m_core,this);

    QTreeWidgetItem *embed_item = new QTreeWidgetItem(this);
    embed_item->setFlags(Qt::ItemIsEnabled);
    addTopLevelItem(embed_item);
    setItemExpanded(embed_item, true);
    setItemWidget(embed_item, 0, m_listView);

    m_listView->setFixedWidth(header()->width());
    m_listView->doItemsLayout();
    const int height = qMax(m_listView->ContentsSize().height() ,1);
    m_listView->setFixedHeight(height);
    this->itemAt(0,0)->setSizeHint(0, QSize(-1, height - 1));

    connect(m_listView,SIGNAL(selectChanged()),this,SIGNAL(selectChange()));
}

void QResourceTreeView::resizeEvent(QResizeEvent *event)
{
    QTreeWidget::resizeEvent(event);
    m_listView->setFixedWidth(header()->width());
    m_listView->doItemsLayout();
    const int height = qMax(m_listView->ContentsSize().height() ,1);
    m_listView->setFixedHeight(height);
    this->itemAt(0,0)->setSizeHint(0, QSize(-1, height - 1));
}

void QResourceTreeView::addFile(QString str)
{    
    m_listView->addFile(str);
    m_listView->setFixedWidth(header()->width());
    m_listView->doItemsLayout();
    const int height = qMax(m_listView->ContentsSize().height() ,1);
    m_listView->setFixedHeight(height);
    this->itemAt(0,0)->setSizeHint(0, QSize(-1, height - 1));
}

void QResourceTreeView::removeFile(QString str)
{
    m_listView->removeFile(str);
    m_listView->setFixedWidth(header()->width());
    m_listView->doItemsLayout();
    const int height = qMax(m_listView->ContentsSize().height() ,1);
    m_listView->setFixedHeight(height);
    this->itemAt(0,0)->setSizeHint(0, QSize(-1, height - 1));
}

void QResourceTreeView::select(QString str)
{
    m_listView->select(str);
}

QString QResourceTreeView::getSelect()
{
    return m_listView->getSelect();
}

void QResourceTreeView::clear()
{
    m_listView->clear();
    m_listView->setFixedWidth(header()->width());
    m_listView->doItemsLayout();
    const int height = qMax(m_listView->ContentsSize().height() ,1);
    m_listView->setFixedHeight(height);
    this->itemAt(0,0)->setSizeHint(0, QSize(-1, height - 1));
}
